package com.antdesignbackend.backend.common.webDriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.HashMap;
import java.util.Map;

import static java.lang.System.setProperty;

public class webDriverUtil {
    public static WebDriver getWebDriver(String ip) {
//        setProperty("webdriver.chrome.driver", "lib/chromedriver");
        setProperty("webdriver.chrome.driver", "/usr/project/chromedriver");
      // setProperty("webdriver.chrome.driver", "lib/chromedriver.exe"); //windows 系统

        WebDriver webDriver;
        ChromeOptions chromeOptions = new ChromeOptions();
        // 设置禁止加载项
        Map<String, Object> prefs = new HashMap<>();
        prefs.put("profile.managed_default_content_settings.images", 2); // 2就是代表禁止加载的意思
        chromeOptions.setExperimentalOption("prefs", prefs);

        Map<String, Object> deviceMetrics = new HashMap<>();
        deviceMetrics.put("width", 360);
        deviceMetrics.put("height", 640);
        deviceMetrics.put("pixelRatio", 3.0);

        Map<String, Object> mobileEmulation = new HashMap<>();
        mobileEmulation.put("deviceMetrics", deviceMetrics);
        mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");

        chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

        //设置无界面，部署linux时设置
        chromeOptions.addArguments("-headless");
        chromeOptions.addArguments("no-sandbox");
        chromeOptions.addArguments("--disable-gpu");


        chromeOptions.addArguments("--proxy-server=http://" + ip);
        webDriver = new ChromeDriver(chromeOptions);
        return webDriver;
    }
}
